<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="">
            <el-input
              v-model="foodName"
              size="medium"
              placeholder="查询"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="success" icon="el-icon-search"
              >查询</el-button
            >
          </el-form-item>
          <el-form-item label="">
            <el-button
              size="small"
              @click="newest(1)"
              type="primary"
              icon="el-icon-circle-plus-outline"
              >新增</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="24">
        <div>
          <el-table
            :data="tableData"
            v-loading="loadFlag"
            border
            style="width: 100%"
          >
            <el-table-column prop="warehouseNumber" label="仓库编号" width="">
            </el-table-column>
            <el-table-column prop="warehouseName" label="仓库名称" width="">
            </el-table-column>
            <!-- <el-table-column prop="address" label="状态"> </el-table-column> -->
            <el-table-column label="操作" width="250" align="center">
              <template #default="scope">
                <el-button size="mini" @click="handleEdit(scope.row, 2)"
                  >编辑</el-button
                >

                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="pagingClass">
            <el-pagination
              :page-sizes="m_page.sizes"
              :page-size="m_page.size"
              :current-page="m_page.number"
              @size-change="m_handleSizeChange"
              @current-change="m_handlePageChange"
              layout="total,sizes,prev, pager, next"
              background
              :total="m_page.totalElements"
            ></el-pagination>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-dialog
      :title="single"
      v-model="plassets"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :inline="true" :model="form" label-width="100px">
        <el-form-item label="仓库编号">
          <el-input
            type="number"
            style="width: 300px"
            v-model="form.warehouse"
          ></el-input>
        </el-form-item>
        <el-form-item label="仓库名称">
          <el-input style="width: 300px" v-model="form.filename"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="plassets = false">取 消</el-button>
          <el-button type="primary" v-if="under == 1" @click="determine()"
            >确 定</el-button
          >
          <el-button @click="hardware()" v-else type="primary"
            >编辑 确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loadFlag: false, //加载flag
      foodName: "",
      plassets: false,
      single: "",
      under: 1,
      form: {
        warehouse: "",
        filename: "",
      },
      tableData: [
        {
          date: "销售",
          name: "张三",
        },
      ],
      underID: "",
      m_page: {
        sizes: [10, 20, 40, 50, 100], //每页最大显示数
        size: 10,
        totalElements: 0,
        totalPages: 3,
        number: 1,
      },
    };
  },
  created() {
    let str = sessionStorage.getItem("token");
    this.headerObj = `Bearer ${str}`;
  },
  beforeMount() {
    this.getRecord();
  },
  methods: {
    //删除
    handleDelete(row) {
      let into = row;
      this.$confirm("确认删除该仓库?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .post(
              `api/serial/WareHouse`,
              {
                id: into.id,
                warehouseNumber: into.warehouseNumber,
                warehouseName: into.warehouseName,
                isDelete: 1,
              },
              {
                headers: {
                  Authorization: this.headerObj,
                },
              }
            )
            .then(() => {
              // console.log(res);
              this.getRecord();
              this.$message.success("删除成功");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //编辑确定
    hardware() {
      this.$axios
        .post(
          `api/serial/WareHouse`,
          {
            id: this.underID,
            warehouseNumber: this.form.warehouse,
            warehouseName: this.form.filename,
          },
          {
            headers: {
              Authorization: this.headerObj,
            },
          }
        )
        .then(() => {
          //   console.log(res);
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.plassets = false;
          this.getRecord();
        })
        .catch(() => {
          this.$message.error("编辑失败");
        });
    },
    //新增仓库
    determine() {
      this.$axios
        .post(
          `api/serial/WareHouse`,
          {
            warehouseNumber: this.form.warehouse,
            warehouseName: this.form.filename,
            // state: 0,
          },
          {
            headers: {
              Authorization: this.headerObj,
            },
          }
        )
        .then(() => {
          //   console.log(res);
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.plassets = false;
          this.getRecord();
        })
        .catch(() => {
          this.$message.error("保存失败");
        });
    },
    getRecord() {
      this.loadFlag = true;
      this.$axios({
        methods: "GET",
        url: "api/serial/WareHouse",
        headers: {
          Authorization: this.headerObj,
        },
        params: {
          pageSize: this.m_page.size,
          pageIndex: this.m_page.number,
          query: {},
        },
      }).then((res) => {
        ({ data: this.tableData, count: this.m_page.totalElements } = res.data);
        // this.tableData.forEach((item) => {
        //   item.startDate = dayjs(item.startDate).format("YYYY-M-D");
        // });
        this.loadFlag = false;
      });
    },

    handleEdit(row, index) {
      this.under = index;
      this.plassets = true;
      this.single = "编辑仓库";
      console.log(row);
      this.underID = row.id;
      this.form.warehouse = row.warehouseNumber;
      this.form.filename = row.warehouseName;
    },
    newest(index) {
      this.under = index;
      this.plassets = true;
      this.single = "新增仓库";
      this.form.warehouse = "";
      this.form.filename = "";
      this.underID = "";
    },
    //页码
    m_handlePageChange(currPage) {
      this.m_page.number = currPage;
      this.getRecord();
    },
    m_handleSizeChange(currSize) {
      this.m_page.size = currSize;
      this.getRecord();
    },
  },
};
</script>

<style scoped>
.addccc {
  margin-bottom: 20px;
}
</style>